package org.magi.sort;

import org.junit.Test;

import java.util.Arrays;

import static org.magi.Utils.printArr;

public class SortTest {

    void printFail(String title, int[] target, int[] correct) {
        System.out.printf("Test fail in %s.\n", title);
        System.out.print("Expect: ");
        printArr(correct);
        System.out.print("Result: ");
        printArr(target);
        throw new RuntimeException("Test fail.");
    }

    void testSort(Sort sort) {
        int[] target = sort.sort(new int[]{});
        int[] correct = new int[]{};
        if (verify(target, correct)) {
            printFail("1", target, correct);
        }

        target = sort.sort(new int[]{5, 4});
        correct = new int[]{4, 5};
        if (verify(target, correct)) {
            printFail("2", target, correct);
        }

        target = sort.sort(new int[]{5});
        correct = new int[]{5};
        if (verify(target, correct)) {
            printFail("3", target, correct);
        }

        target = sort.sort(null);
        if (verify(target, null)) {
            printFail("4", target, null);
        }

        target = sort.sort(new int[]{0, 1, 2, 3, 4, 5});
        correct = new int[]{0, 1, 2, 3, 4, 5};
        if (verify(target, correct)) {
            printFail("5", target, correct);
        }

        target = sort.sort(new int[]{5, 4, 3, 2, 1, 0});
        correct = new int[]{0, 1, 2, 3, 4, 5};
        if (verify(target, correct)) {
            printFail("6", target, correct);
        }

        target = sort.sort(new int[]{4, 3, 2, 1, 0});
        correct = new int[]{0, 1, 2, 3, 4};
        if (verify(target, correct)) {
            printFail("7", target, correct);
        }

        target = sort.sort(new int[]{-1, 2, -3, 0, 4, -5});
        correct = new int[]{-5, -3, -1, 0, 2, 4};
        if (verify(target, correct)) {
            printFail("8", target, correct);
        }
    }

    boolean verify(int[] target, int[] correct){
        return !Arrays.equals(target, correct);
    }

    @Test
    public void testBubble() {
        testSort(new Bubble());
    }

    @Test
    public void testQuickSort() {
        testSort(new QuickSort());
    }

    @Test
    public void testMergeSort() {
        testSort(new MergeSort());
    }
}
